package com.luck.easyfoodbatch.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.luck.easyfoodbatch.App;
import com.luck.easyfoodbatch.Constant;
import com.luck.easyfoodbatch.R;
import com.luck.easyfoodbatch.dialog.CustomDialog;
import com.luck.easyfoodbatch.mvp.model.RefundM;
import com.luck.easyfoodbatch.ui.ChatActivity;
import com.luck.easyfoodbatch.util.HttpUtil;
import com.luck.easyfoodbatch.util.Tools;
import com.luck.easyrecyclerview.adapter.BaseViewHolder;
import com.luck.easyrecyclerview.adapter.RecyclerArrayAdapter;

/**
 * author：luck
 * project：LoveCare
 * package：com.tongyu.luck.lovecare.adapter
 * email：893855882@qq.com
 * data：16/8/12
 */
public class ReturnAdapter extends RecyclerArrayAdapter<RefundM.DataBean.ModelsBean> {
    private Context mContext;

    public ReturnAdapter(Context context) {
        super(context);
        this.mContext = context;
    }

    @Override
    public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {

        return new CouponViewHolder(parent);
    }

    public class CouponViewHolder extends BaseViewHolder<RefundM.DataBean.ModelsBean> {
        TextView tv_shop_name;
        TextView tv_status;
        TextView tv_title;
        TextView tv_spec;
        ImageView iv_banner;
        TextView tv_money;
        TextView tv_return_money;
        TextView tv_phone;

        public CouponViewHolder(ViewGroup parent) {
            super(parent, R.layout.return_item);
            tv_shop_name = $(R.id.tv_shop_name);
            tv_status = $(R.id.tv_status);
            tv_title = $(R.id.tv_title);
            tv_spec = $(R.id.tv_spec);
            iv_banner = $(R.id.iv_banner);
            tv_money = $(R.id.tv_money);
            tv_return_money = $(R.id.tv_return_money);
            tv_phone = $(R.id.tv_phone);
        }

        @Override
        public void setData(final RefundM.DataBean.ModelsBean bean, int position) {
            String shop_name = bean.getShop_name();
            String goods_name = bean.getGoods_name();
            String goods_avatar = bean.getGoods_avatar();
            int goods_num = bean.getGoods_num();
            String specifications = bean.getGoods_specifications();
            String goods_package = bean.getGoods_package();
            int status = bean.getStatus();
            String order_price = bean.getOrder_price();
            String refund_price = bean.getRefund_price();
            //退款状态 1处理中 2已同意 3已拒绝 4退款成功
            if (!Tools.isNull(shop_name)) {
                tv_shop_name.setText(shop_name);
            } else {
                tv_shop_name.setText("");
            }
            switch (status) {
                case 1:
                    tv_status.setText("处理中");
                    break;
                case 2:
                    tv_status.setText("已同意");
                    break;
                case 3:
                    tv_status.setText("已拒绝");
                    break;
                case 4:
                    tv_status.setText("退款成功");
                    break;
            }

            if (!Tools.isNull(goods_name)) {
                tv_title.setText(goods_name);
            } else {
                tv_title.setText("");
            }
            tv_money.setText("交易金额 ￥" + order_price);
            tv_return_money.setText("退款金额 ￥" + refund_price);
            tv_spec.setText(goods_num + specifications + "/" + goods_package);

            if (!Tools.isNull(goods_avatar)) {
                String path = "";
                if (goods_avatar.startsWith("http")) {
                    path = goods_avatar;
                } else {
                    path = HttpUtil.IMG_THUMB_BASE_URL + goods_avatar;
                }
                Glide.with(mContext)
                        .load(path)
                        .centerCrop()
                        .diskCacheStrategy(DiskCacheStrategy.ALL)
                        .into(iv_banner);
            } else {
                iv_banner.setImageResource(R.mipmap.loading_4b3);
            }

            tv_phone.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
//                    Intent intent = new Intent();
//                    Bundle bundle = new Bundle();
//                    bundle.putString(Constant.EXTRA_USER_ID, bean.getShop_hx());
//                    bundle.putString(Constant.EXTRA_NICK_ID, bean.getShop_name());
//                    intent.setClass(mContext, ChatActivity.class);
//                    intent.putExtras(bundle);
//                    mContext.startActivity(intent);
                    showPhoneDialog();

                }
            });
        }
    }

    /**
     * 拨打电话
     */
    private void showPhoneDialog() {
        final CustomDialog dialog = new CustomDialog(mContext, App.screenWidth * 3 / 4,
                App.screenHeight / 4, R.layout.wind_base_dialog_xml, R.style.Theme_dialog);
        Button btn_cancel = (Button) dialog.findViewById(R.id.btn_cancel);
        Button btn_commit = (Button) dialog.findViewById(R.id.btn_commit);
        TextView tv_title = (TextView) dialog.findViewById(R.id.tv_title);
        TextView tv_content = (TextView) dialog.findViewById(R.id.tv_content);
        tv_title.setText("联系客服");
        btn_commit.setText("呼叫");
        tv_content.setText(HttpUtil.CALL_PHONE);
        btn_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });
        btn_commit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Tools.callPhone(HttpUtil.CALL_PHONE, mContext);
                dialog.dismiss();
            }
        });
        dialog.show();
    }
}
